<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>API测试工具 - 工单管理系统</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet">
    <style>
        body {
            background-color: #f8f9fa;
        }
        .api-test-card {
            background: white;
            border-radius: 10px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
            margin-bottom: 20px;
        }
        .api-endpoint {
            background: #e9ecef;
            padding: 10px;
            border-radius: 5px;
            font-family: monospace;
            margin-bottom: 15px;
        }
        .response-area {
            background: #f8f9fa;
            border: 1px solid #dee2e6;
            border-radius: 5px;
            padding: 15px;
            min-height: 200px;
            font-family: monospace;
            white-space: pre-wrap;
        }
        .btn-test {
            margin-right: 10px;
            margin-bottom: 10px;
        }
    </style>
</head>
<body>
    <div class="container mt-4">
        <div class="row">
            <div class="col-12">
                <h1 class="mb-4"><i class="fas fa-tools me-2"></i>API测试工具</h1>
                
                <!-- 用户注册测试 -->
                <div class="api-test-card">
                    <div class="card-header">
                        <h5><i class="fas fa-user-plus me-2"></i>用户注册测试</h5>
                    </div>
                    <div class="card-body">
                        <div class="api-endpoint">POST /api/auth/register</div>
                        <div class="row">
                            <div class="col-md-6">
                                <h6>测试数据：</h6>
                                <div class="mb-3">
                                    <label class="form-label">用户名</label>
                                    <input type="text" class="form-control" id="regUsername" value="testuser">
                                </div>
                                <div class="mb-3">
                                    <label class="form-label">密码</label>
                                    <input type="password" class="form-control" id="regPassword" value="Test123">
                                </div>
                                <div class="mb-3">
                                    <label class="form-label">确认密码</label>
                                    <input type="password" class="form-control" id="regConfirmPassword" value="Test123">
                                </div>
                                <div class="mb-3">
                                    <label class="form-label">真实姓名</label>
                                    <input type="text" class="form-control" id="regFullName" value="测试用户">
                                </div>
                                <div class="mb-3">
                                    <label class="form-label">邮箱</label>
                                    <input type="email" class="form-control" id="regEmail" value="test@example.com">
                                </div>
                                <div class="mb-3">
                                    <label class="form-label">角色</label>
                                    <select class="form-control" id="regRole">
                                        <option value="CUSTOMER">客户</option>
                                        <option value="AGENT">客服</option>
                                    </select>
                                </div>
                                <button class="btn btn-primary btn-test" onclick="testRegister()">
                                    <i class="fas fa-play me-2"></i>测试注册
                                </button>
                            </div>
                            <div class="col-md-6">
                                <h6>响应结果：</h6>
                                <div class="response-area" id="registerResponse">点击测试按钮查看结果...</div>
                            </div>
                        </div>
                    </div>
                </div>

                <!-- 用户登录测试 -->
                <div class="api-test-card">
                    <div class="card-header">
                        <h5><i class="fas fa-sign-in-alt me-2"></i>用户登录测试</h5>
                    </div>
                    <div class="card-body">
                        <div class="api-endpoint">POST /api/auth/login</div>
                        <div class="row">
                            <div class="col-md-6">
                                <h6>测试数据：</h6>
                                <div class="mb-3">
                                    <label class="form-label">用户名</label>
                                    <input type="text" class="form-control" id="loginUsername" value="admin">
                                </div>
                                <div class="mb-3">
                                    <label class="form-label">密码</label>
                                    <input type="password" class="form-control" id="loginPassword" value="admin123">
                                </div>
                                <button class="btn btn-success btn-test" onclick="testLogin()">
                                    <i class="fas fa-play me-2"></i>测试登录
                                </button>
                                <button class="btn btn-info btn-test" onclick="testLoginWithNewUser()">
                                    <i class="fas fa-user me-2"></i>使用新用户登录
                                </button>
                            </div>
                            <div class="col-md-6">
                                <h6>响应结果：</h6>
                                <div class="response-area" id="loginResponse">点击测试按钮查看结果...</div>
                            </div>
                        </div>
                    </div>
                </div>

                <!-- 获取用户信息测试 -->
                <div class="api-test-card">
                    <div class="card-header">
                        <h5><i class="fas fa-user me-2"></i>获取用户信息测试</h5>
                    </div>
                    <div class="card-body">
                        <div class="api-endpoint">GET /api/auth/me</div>
                        <div class="row">
                            <div class="col-md-6">
                                <h6>测试说明：</h6>
                                <p>需要先登录获取Token，然后使用Token访问此接口</p>
                                <div class="mb-3">
                                    <label class="form-label">Token</label>
                                    <textarea class="form-control" id="userToken" rows="3" placeholder="从登录接口获取的Token"></textarea>
                                </div>
                                <button class="btn btn-warning btn-test" onclick="testGetUserInfo()">
                                    <i class="fas fa-play me-2"></i>测试获取用户信息
                                </button>
                            </div>
                            <div class="col-md-6">
                                <h6>响应结果：</h6>
                                <div class="response-area" id="userInfoResponse">点击测试按钮查看结果...</div>
                            </div>
                        </div>
                    </div>
                </div>

                <!-- 快速测试 -->
                <div class="api-test-card">
                    <div class="card-header">
                        <h5><i class="fas fa-bolt me-2"></i>快速测试</h5>
                    </div>
                    <div class="card-body">
                        <p>一键测试完整的注册-登录-获取信息流程</p>
                        <button class="btn btn-primary btn-lg" onclick="quickTest()">
                            <i class="fas fa-rocket me-2"></i>开始快速测试
                        </button>
                        <div class="mt-3">
                            <div class="response-area" id="quickTestResponse">点击开始快速测试...</div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script>
        const API_BASE_URL = 'http://localhost:9090/api';
        let currentToken = '';

        // 测试注册
        async function testRegister() {
            const responseDiv = document.getElementById('registerResponse');
            responseDiv.textContent = '正在测试注册...';
            
            const data = {
                username: document.getElementById('regUsername').value,
                password: document.getElementById('regPassword').value,
                confirmPassword: document.getElementById('regConfirmPassword').value,
                fullName: document.getElementById('regFullName').value,
                email: document.getElementById('regEmail').value,
                role: document.getElementById('regRole').value
            };

            try {
                const response = await fetch(`${API_BASE_URL}/auth/register`, {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify(data)
                });

                const result = await response.json();
                responseDiv.textContent = JSON.stringify(result, null, 2);
            } catch (error) {
                responseDiv.textContent = `错误: ${error.message}`;
            }
        }

        // 测试登录
        async function testLogin() {
            const responseDiv = document.getElementById('loginResponse');
            responseDiv.textContent = '正在测试登录...';
            
            const data = {
                username: document.getElementById('loginUsername').value,
                password: document.getElementById('loginPassword').value
            };

            try {
                const response = await fetch(`${API_BASE_URL}/auth/login`, {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify(data)
                });

                const result = await response.json();
                responseDiv.textContent = JSON.stringify(result, null, 2);
                
                if (result.success && result.data.token) {
                    currentToken = result.data.token;
                    document.getElementById('userToken').value = currentToken;
                }
            } catch (error) {
                responseDiv.textContent = `错误: ${error.message}`;
            }
        }

        // 使用新用户登录
        async function testLoginWithNewUser() {
            document.getElementById('loginUsername').value = document.getElementById('regUsername').value;
            document.getElementById('loginPassword').value = document.getElementById('regPassword').value;
            await testLogin();
        }

        // 测试获取用户信息
        async function testGetUserInfo() {
            const responseDiv = document.getElementById('userInfoResponse');
            const token = document.getElementById('userToken').value || currentToken;
            
            if (!token) {
                responseDiv.textContent = '请先登录获取Token';
                return;
            }

            responseDiv.textContent = '正在获取用户信息...';

            try {
                const response = await fetch(`${API_BASE_URL}/auth/me`, {
                    method: 'GET',
                    headers: {
                        'Authorization': `Bearer ${token}`
                    }
                });

                const result = await response.json();
                responseDiv.textContent = JSON.stringify(result, null, 2);
            } catch (error) {
                responseDiv.textContent = `错误: ${error.message}`;
            }
        }

        // 快速测试
        async function quickTest() {
            const responseDiv = document.getElementById('quickTestResponse');
            responseDiv.textContent = '开始快速测试...\n\n';
            
            try {
                // 1. 测试注册
                responseDiv.textContent += '1. 测试用户注册...\n';
                const registerData = {
                    username: `testuser${Date.now()}`,
                    password: 'Test123',
                    confirmPassword: 'Test123',
                    fullName: '测试用户',
                    email: `test${Date.now()}@example.com`,
                    role: 'CUSTOMER'
                };

                const registerResponse = await fetch(`${API_BASE_URL}/auth/register`, {
                    method: 'POST',
                    headers: { 'Content-Type': 'application/json' },
                    body: JSON.stringify(registerData)
                });

                const registerResult = await registerResponse.json();
                responseDiv.textContent += `注册结果: ${registerResult.success ? '成功' : '失败'}\n`;
                if (!registerResult.success) {
                    responseDiv.textContent += `错误: ${registerResult.message}\n`;
                    return;
                }

                // 2. 测试登录
                responseDiv.textContent += '\n2. 测试用户登录...\n';
                const loginData = {
                    username: registerData.username,
                    password: registerData.password
                };

                const loginResponse = await fetch(`${API_BASE_URL}/auth/login`, {
                    method: 'POST',
                    headers: { 'Content-Type': 'application/json' },
                    body: JSON.stringify(loginData)
                });

                const loginResult = await loginResponse.json();
                responseDiv.textContent += `登录结果: ${loginResult.success ? '成功' : '失败'}\n`;
                if (!loginResult.success) {
                    responseDiv.textContent += `错误: ${loginResult.message}\n`;
                    return;
                }

                const token = loginResult.data.token;
                currentToken = token;

                // 3. 测试获取用户信息
                responseDiv.textContent += '\n3. 测试获取用户信息...\n';
                const userInfoResponse = await fetch(`${API_BASE_URL}/auth/me`, {
                    method: 'GET',
                    headers: { 'Authorization': `Bearer ${token}` }
                });

                const userInfoResult = await userInfoResponse.json();
                responseDiv.textContent += `获取用户信息结果: ${userInfoResult.success ? '成功' : '失败'}\n`;

                // 4. 测试获取权限
                responseDiv.textContent += '\n4. 测试获取用户权限...\n';
                const permissionsResponse = await fetch(`${API_BASE_URL}/auth/permissions`, {
                    method: 'GET',
                    headers: { 'Authorization': `Bearer ${token}` }
                });

                const permissionsResult = await permissionsResponse.json();
                responseDiv.textContent += `获取权限结果: ${permissionsResult.success ? '成功' : '失败'}\n`;

                responseDiv.textContent += '\n✅ 快速测试完成！所有接口都正常工作。\n';
                responseDiv.textContent += `\n用户信息:\n${JSON.stringify(userInfoResult.data, null, 2)}\n`;
                responseDiv.textContent += `\n权限列表:\n${JSON.stringify(permissionsResult.data, null, 2)}\n`;

            } catch (error) {
                responseDiv.textContent += `\n❌ 测试过程中出现错误: ${error.message}\n`;
            }
        }

        // 页面加载时检查API连接
        document.addEventListener('DOMContentLoaded', async () => {
            try {
                const response = await fetch(`${API_BASE_URL}/auth/roles`);
                if (response.ok) {
                    console.log('✅ API连接正常');
                } else {
                    console.log('❌ API连接异常');
                }
            } catch (error) {
                console.log('❌ 无法连接到API:', error.message);
            }
        });
    </script>
</body>
</html>
